package cn.dnui_zzh807.dnuifood_16110100807.controller;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.widget.Toast;

import java.util.List;

import cn.dnui_zzh807.dnuifood_16110100807.R;
import cn.dnui_zzh807.dnuifood_16110100807.adapter.FoodListAdapter;
import cn.dnui_zzh807.dnuifood_16110100807.bean.FoodInfo;
import cn.dnui_zzh807.dnuifood_16110100807.listener.OnRetrofitListener;
import cn.dnui_zzh807.dnuifood_16110100807.model.FoodModel;

public class SearchResultActivity extends AppCompatActivity implements OnRetrofitListener<List<FoodInfo>> {
      private RecyclerView recyclerView;
      private String search;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_result);

      initViews();
      initData();

    }

    void initViews(){
        recyclerView = (RecyclerView)findViewById(R.id.search_rv);
        //recyclerView.setLayoutManager(new LinearLayoutManager(this));
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
    }
    void initEvents(){
        //处理返回按钮
    }

    void initData(){
//        Intent intent = getIntent();
//        search = intent.getStringExtra(search);
        search = getIntent().getStringExtra("search");
        FoodModel foodModel = new FoodModel();
        foodModel.getFoodBySearch(search,this);
    }
    @Override
    public void onSuccess(List<FoodInfo> foodInfos, int flag) {

        if (foodInfos!=null&&foodInfos.size()>0){
            FoodListAdapter adapter = new FoodListAdapter(SearchResultActivity.this,foodInfos);
            recyclerView.setAdapter(adapter);
        }else {
            Toast.makeText(SearchResultActivity.this,"没有搜索结果",Toast.LENGTH_SHORT).show();
            finish();
        }

    }

    @Override
    public void onFail() {
        Toast.makeText(SearchResultActivity.this,"搜索网络错误",Toast.LENGTH_SHORT).show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //创建菜单
        getMenuInflater().inflate(R.menu.food_list_collect,menu);

        //菜单创建完毕后向服务器发送请求，判断是否已经收藏
        menu.findItem(R.id.item_collect1).setTitle("搜索结果");
        return super.onCreateOptionsMenu(menu);
    }

}
